package ds.factcom.view.ui.product;	

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import ds.factcom.controller.product.ProductController;
import ds.factcom.domain.product.Product;
import ds.factcom.exception.EmptyStringException;
import ds.factcom.exception.ExistDomainException;
import ds.factcom.exception.FloatNumberFormatException;
import ds.factcom.exception.NotExistDomainException;
import ds.factcom.view.ui.Messages;
import ds.factcom.view.ui.StateDomain;
import ds.factcom.view.validate.ValidatorProduct;

@SuppressWarnings("serial")
public class ProductView extends JDialog {

	private JPanel textPanel;
	private JLabel descriptionLabel; 
	private JLabel priceLabel; 
	private JLabel idProductLabel;
	
	private JPanel buttonsPanel;
	private JTextField idProductTF;
	private JTextField descriptionTF;
	private JTextField priceTF;
	
	private JButton acceptBtn;
	private JButton cancelBtn;
	private JButton searchBtn;
	
	private ProductController productController;
	private StateDomain title;
	
	public ProductView(ProductController productController, StateDomain title) {
		this.productController = productController;
		this.title = title;
		initGUI();
	}

	private void initGUI() {
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setLayout(new BorderLayout());
		setLocation(400, 200);
		setSize(400, 150);
		setResizable(false);
		setTitle(title + "Product");
		setModal(true);
		add(getTextPanel(), BorderLayout.CENTER);
		add(getButtonsPanel(), BorderLayout.SOUTH);
		enabledButtons(title);
		enabledTextField(title);
	}
//*************************  Panel de Campos ************************************++
	
	private JPanel getTextPanel() {
		if (textPanel == null) {
			textPanel = new JPanel(new GridLayout(3,2));
			textPanel.add(getIdProductLabel());
			textPanel.add(getIdProductTF());
			textPanel.add(getDescriptionLabel());
			textPanel.add(getDescriptionTF());
			textPanel.add(getPriceLabel());
			textPanel.add(getPriceTF());
		}
		return textPanel;
	}

	private JLabel getDescriptionLabel() {
		if (descriptionLabel == null) {
			descriptionLabel = new JLabel("Descripcion");
		}
		return descriptionLabel;
	}

	private JLabel getPriceLabel() {
		if (priceLabel == null) {
			priceLabel = new JLabel("Precio");
		}
		return priceLabel;
	}

	private JLabel getIdProductLabel() {
		if (idProductLabel == null) {
			idProductLabel = new JLabel("ID producto");
		}
		return idProductLabel;
	}

	private JTextField getIdProductTF() {
		if (idProductTF == null) {
			idProductTF = new JTextField();
			idProductTF.setEnabled(false);
		}
		return idProductTF;
	}

	private JTextField getPriceTF() {
		if (priceTF == null) {
			priceTF = new JTextField();
		}
		return priceTF;
	}

	private JTextField getDescriptionTF() {
		if (descriptionTF == null) {
			descriptionTF = new JTextField();
		}
		return descriptionTF;
	}

//*************************  Botonera ************************************++
	private JPanel getButtonsPanel() {
		if (buttonsPanel == null) {
			buttonsPanel = new JPanel(new GridLayout(1,3));
			buttonsPanel.add(getSearchBtn());
			buttonsPanel.add(getAcceptBtn());
			buttonsPanel.add(getCancelBtn());
		}
		return buttonsPanel;
	}

	private JButton getAcceptBtn() {
		if (acceptBtn == null) {
			acceptBtn = new JButton("Guardar");
			acceptBtn.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						String description = getDescriptionTF().getText();
						String price = getPriceTF().getText();
						ValidatorProduct.isNotEmpty(description);
						ValidatorProduct.isFloat(price);

						if (productController.getProduct() != null) {
							productController.modify(Float.valueOf(price));
						} else {
							productController.insert(description, Float.valueOf(price));
						}
						productController.setProduct(null);
						Messages.showInformationDialog(ProductView.this, "El Producto se ha guardado correctamente", "Informacion Producto");
						if (Messages.showConfirmDialog(ProductView.this, "Desea " + title + " otro Producto ?", "Informacion Producto") 
								== JOptionPane.NO_OPTION) {
							ProductView.this.dispose();
						} else {
							enabledButtons(title);
							cleanField();
						}
					} catch (ExistDomainException e1) {
						Messages.showErrorMessage(ProductView.this, e1.getMessage());
					} catch (EmptyStringException e1) {
						Messages.showErrorMessage(ProductView.this, e1.getMessage());
					} catch (FloatNumberFormatException e1) {
						Messages.showErrorMessage(ProductView.this, e1.getMessage());
					}
				}
			});
		}
		return acceptBtn;
	}

	private JButton getCancelBtn() {
		if (cancelBtn == null) {
			cancelBtn = new JButton("Cancelar");
			cancelBtn.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					cleanField();
					enabledButtons(title);
				}
			});
		}
		return cancelBtn;
	}
	
	private JButton getSearchBtn() {
		if (searchBtn == null) {
			searchBtn = new JButton("Buscar");
			searchBtn.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						new SearchProductView(productController).open();
						cleanField();
						enabledButtons(true);
						ProductView.this.repaint();
					} catch (NotExistDomainException e1) {
						Messages.showErrorMessage(e1.getMessage());
						productController.setProduct(null);
					} catch (EmptyStringException e1) {
						Messages.showErrorMessage(e1.getMessage());
						productController.setProduct(null);
					}
				}
			});
		}
		return searchBtn;
	}

	public void open() {
		setVisible(true);
	}
	
	private void enabledTextField(StateDomain stateDomain) {
		if (stateDomain.equals(StateDomain.INSERT)) {
			getDescriptionTF().setEnabled(true);
		} else if (stateDomain.equals(StateDomain.MODIFY)) {
			getDescriptionTF().setEnabled(false);
		}
	}

	private void enabledButtons(StateDomain stateDomain) {
		if (stateDomain.equals(StateDomain.INSERT)) {
			enabledButtons(true);
		} else if (stateDomain.equals(StateDomain.MODIFY)) {
			enabledButtons(false);
		}
	}

	private void enabledButtons(boolean enabled) {
		getAcceptBtn().setEnabled(enabled);
		getCancelBtn().setEnabled(enabled);
		getSearchBtn().setEnabled(!enabled);
	}

	private void cleanField() {
		Product product = productController.getProduct();
		if (product != null) {
			getIdProductTF().setText(String.valueOf(product.getId()));
			getDescriptionTF().setText(product.getDescription());
			getPriceTF().setText(String.valueOf(product.getPrice()));
		} else {
			getIdProductTF().setText("");
			getDescriptionTF().setText("");
			getPriceTF().setText("");
		}
		enabledTextField(title);
	}
}